/**
* 名称 ：饭邦德 DatePicker 组件[基于JQUERY版]
* 作者 ：刘鑫锐
* 版本 ：1.0 beta
*		1.0 ，2008-11-11 ，刘鑫锐
* 说明 ： $cfg 是 日期组件的全局属性区，
*		$cfg 能有效的保持 日期控件 的状态
*		构造函数使用使用apply函数把外部参数拷贝到$cfg
*/
var FbdDatePicker = function(props) {
	var $cfg = {
		//外部设置的属性
		panel_id : null,//用于显示控件的层
		image_dir : "../images/new_preorder/",//图片目录
		//回调函数
		on_date_item_selected : null,
		//内容使用的属性
		current_date : null,
		selected_date : null,
		month_of_ping_year : [31,28,31,30,31,30,31,31,30,31,30,31],
		month_of_run_year : [31,29,31,30,31,30,31,31,30,31,30,31]
	}
	init();
	function init() {
		apply($cfg,props);
	}
	//公有函数
	//显示某个月
	this.showMonth = function(year,month) {
		//年 ，月不能为空
		$cfg.current_date = new Date();
		$cfg.current_date.setFullYear(year);
		$cfg.current_date.setMonth(month-1);
		var html = buildHtml();
		$("#"+$cfg.panel_id).html(html);
	}
	//高亮有效的日期项
	this.setUseFulItem = function(begin, end) {
		var date = new Date();
		date.setFullYear(begin.getFullYear());
		date.setMonth(begin.getMonth());
		date.setDate(begin.getDate());
		var items = $("[@use='date_picker_item']",$("#"+$cfg.panel_id));
		items.parent().removeClass();
		items.parent().addClass("unuseful_num");
		while(date <= end) {
			$("#"+getDateItemId(date)).parent().removeClass("unuseful_num");
			$("#"+getDateItemId(date)).parent().addClass("useful_num");
			date.setDate(date.getDate() + 1);
		}
		setEvent();
	}
	//参数：日期，取该日期所在月份的最后一天
	this.getMonthEndDate = function(date) {
		if(isRunYear(date.getFullYear())) {
			//闰年
			return $cfg.month_of_run_year[date.getMonth()];
		} else {
			//平年
			return $cfg.month_of_ping_year[date.getMonth()];
		}
	}
	//
	this.setChoiceItem = function(date) {
		$cfg.selected_date = date;
		var sid = getDateItemId($cfg.selected_date);
		$("#"+sid).parent().addClass("choiced_num");
	}
	//
	this.setTodayItem = function(today) {
		var sid = getDateItemId(today);
		$("#"+sid).css("color","red");
	}
	//获得当前选中的日期
	this.getSelectedDate = function() {
		return $cfg.selected_date;
	}
	//显示控件
	this.show = function() {
		$("#"+$cfg.panel_id).show();
	}
	//隐藏控件
	this.hide = function() {
		$("#"+$cfg.panel_id).hide();
	}
 	//私有函数
	function setEvent() {
		
		var items = $(".useful_num");
		
		items.hover(function(){
			var date_item = $("[@use='date_picker_item']",$(this));
			$(this).css("cursor","pointer");
			if(getDateItemId($cfg.selected_date) == date_item.attr("id")) {
			} else {
				$(this).addClass("choiced_num");				
			}
		},function(){
			var date_item = $("[@use='date_picker_item']",$(this));
			if(getDateItemId($cfg.selected_date) == date_item.attr("id")) {
			} else {
				$(this).removeClass("choiced_num");
			}
		});
		
		items.click(function() {
			var item = $("[@use='date_picker_item']",$(this));
			var year = item.attr("year");
			var month = item.attr("month");
			var date = item.attr("date");
			var day = item.attr("day");
			var d = new Date();
			d.setFullYear(year);
			d.setMonth(month-1);
			d.setDate(date);
			$cfg.selected_date = d;
			items.removeClass("choiced_num");
			items.addClass("useful_num");
			$(this).addClass("choiced_num");
			$cfg.on_date_item_selected(year,month,date,day,item);
		});
	}
	//
	function buildHtml() {
		var html = "";
		var date = $cfg.current_date;
		date.setDate(1);
		var curr_month_end_date = 0;
		var is_curr_month = true;
		if(isRunYear(date.getFullYear())) {
			//如果是闰年
			curr_month_end_date = $cfg.month_of_run_year[date.getMonth()];
		} else {
			curr_month_end_date = $cfg.month_of_ping_year[date.getMonth()];
		}
		html += '<table width="214" border="0" cellpadding="0" cellspacing="2" style="border:1px solid #c0c0c0">';
		//年.月份
		html += '<tr><td height="18" colspan="7" align="center" background="'+$cfg.image_dir+'calendar_top.gif">';//
		html += '<strong>'+date.getFullYear()+'.'+(date.getMonth()+1)+'</strong>';
		html += '</td></tr>';
		//星期几
		html += getColHeads();
		for(var week=1; week<=6; week++) {//for week in month
			if(week == 1) {
				html += "<tr>";
				for(var day=0; day<=6; day++) {//for day in week
					if(day == date.getDay()) {
						html += getNumItem(date);
						date.setDate(date.getDate()+1);
					} else {
						html += getNoNumItem();
					}
				}
				html += "</tr>";			
			} else {
				html += "<tr>";
				for(var day=0; day<=6; day++) {//for day in week
					if(is_curr_month) {
						if(day == date.getDay()) {
							if(date.getDate() <= curr_month_end_date) {
								html += getNumItem(date);
								date.setDate(date.getDate()+1);
								if(date.getDate() == 1) {
									is_curr_month = false;
								}
							} else {
								html += getNoNumItem();
							}
						} else {
							html += getNoNumItem();
						}
					} else {
						html += getNoNumItem();
					}
				}
				html += "</tr>";			
			}
		}
		html += "</table>";
		return html;
	}
	//列头，星期几
	function getColHeads() {
		var heads = "";
		heads += '<tr>';
		heads += '<td width="30" height="16" align="center" valign="middle" bgcolor="#8a8a8a" class="whitebold">日</td>';
		heads += '<td width="30" height="16" align="center" valign="middle" bgcolor="#8a8a8a" class="whitebold">一</td>';
		heads += '<td width="30" height="16" align="center" valign="middle" bgcolor="#8a8a8a" class="whitebold">二</td>';
		heads += '<td width="30" height="16" align="center" valign="middle" bgcolor="#8a8a8a" class="whitebold">三</td>';
		heads += '<td width="30" height="16" align="center" valign="middle" bgcolor="#8a8a8a" class="whitebold">四</td>';
		heads += '<td width="30" height="16" align="center" valign="middle" bgcolor="#8a8a8a" class="whitebold">五</td>';
		heads += '<td width="30" height="16" align="center" valign="middle" bgcolor="#8a8a8a" class="whitebold">六</td>';
		heads += '</tr>';
		return heads;
	}
	//没有数字的项
	function getNoNumItem() {
		return '<td width="30" height="21" align="center" class="nonum">&nbsp;</td>';
	}
	//数字项
	function getNumItem(date) {
		var sid = getDateItemId(date);
		var year_num = date.getFullYear();
		var month_num = date.getMonth()+1;
		var date_num = date.getDate();
		var day_num = date.getDay();
		var item = "";
		item += '<td width="30" height="21" align="center" class="unuseful_num">'
		item += '<span id="'+sid+'" use="date_picker_item" year="'+year_num+'" month="'+month_num+'" date="'+date_num+'" day="'+day_num+'">'+date_num+'</span>';
		item += '</td>';
		return item;
	}
	//检查是不是闰年
	function isRunYear(year) {
		var is_run_year = false;
		if(year % 4 == 0) {
			//首先必须满足能被四整除
			is_run_year = true;
			if((year % 100 == 0) && (year % 400 != 0)) {
				//然后排除能被100整除，但不能被400整除的年份
				is_run_year = false; 
			}
		}
		return is_run_year;
	}
	//当前日期SPAN的ID
	function getDateItemId(date) {
		if(date == null) {
			return false;
		}
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		var date = date.getDate();
		return formatStr("date_{0}_{1}_{2}",year,month,date);
	}
	//拷贝属性
	function apply(o, c){
		if(o && c && typeof c == 'object'){
			for(var p in c){
				o[p] = c[p];
			}
		}
		return o;
	}
	//格式化字符串 like "{0},{1},{2}"
	function formatStr(format) {
		var args = Array.prototype.slice.call(arguments, 1);
		return format.replace(/\{(\d+)\}/g, function(m, i){
			return args[i];
		});
	}
}